#!/bin/bash
IP=""

source common.sh

function usage()
{
    echo "`basename $0` [script_name|all]"
}

function create_Fedora29_Update()
{
    PROJECT="$1"
    REPO=$2
    shift 2
    ARCHLIST=$@

    META="$PROJECT.meta"
    sed -i '/<person userid/d' $META
    sed -i '/<group groupid/d' $META
    sed -i '/<description\/>/a\  <person userid="Admin" role="bugowner"/>\n  <person userid="Admin" role="maintainer"/>' $META
    PRO_CONF="$PROJECT.prjconf"
    osc meta prj "$PROJECT" --file="$META"
    osc meta prjconf "$PROJECT" --file="$PRO_CONF"
    for line in $ARCHLIST
    do
        mkdir -p $BUILD_PATH/$PROJECT/$REPO/$line/:full
    if [ -d ${PROJECT//:/}/$REPO/$line/\:full ]
    then
        cp -rf ${PROJECT//:/}/$REPO/$line/\:full/* $BUILD_PATH/$PROJECT/$REPO/$line/\:full
    fi
        chown -R obsrun.obsrun $BUILD_PATH/$PROJECT/
        /usr/lib/obs/server/bs_admin --rescan-repository $PROJECT $REPO $line
    done

}

function modify_pkg_meta()
{
    set +e
    local prj_name="$1"
    local pkg_name="$2"
    local conf_type="$3"
    shift 3

    if [ -f "${prj_name//:/}/$pkg_name/.osc/_meta" ];then
        cp ${prj_name//:/}/$pkg_name/.osc/_meta ${prj_name//:/}/$pkg_name/.osc/_meta-tmp
        grep "<$conf_type>" ${prj_name//:/}/$pkg_name/.osc/_meta-tmp &>/dev/null
        if [ $? -eq 0 ]
        then
            line1=`grep -n "<$conf_type>" ${prj_name//:/}/$pkg_name/.osc/_meta-tmp | cut -d \: -f 1`
            line2=`grep -n "</"$conf_type">" ${prj_name//:/}/$pkg_name/.osc/_meta-tmp | cut -d \: -f 1`
            sed -i "$line1,${line2}d" ${prj_name//:/}/$pkg_name/.osc/_meta-tmp
            sed -i "${line1}i$@" ${prj_name//:/}/$pkg_name/.osc/_meta-tmp
        else
            line1=`grep -n "<description/>" ${prj_name//:/}/$pkg_name/.osc/_meta-tmp | cut -d \: -f 1`
            sed -i "${line1}a$@" ${prj_name//:/}/$pkg_name/.osc/_meta-tmp
        fi
        osc meta pkg "$prj_name" "$pkg_name" --file="${prj_name//:/}/$pkg_name/.osc/_meta-tmp"
    fi
    set -e
}

function fixed_prj_failed_pkg()
{
    local prj_name=""
    local ret=0
    for prj_name in $@
    do
        while true
        do
            ret=`expr $ret + 1`
            local failed_list=`osc results $prj_name --csv | grep failed |sed  "s/;failed//g"`
            if [ -z "$failed_list" ];then
                break
            fi
            osc rebuild $prj_name -f
            wait_build_finish $prj_name
            if [ $ret -gt 3 ]
            then
                break
            fi
        done
    done
}

function analysis_agent()
{
    A_ALL="$1"
    A_IP=${A_ALL#*"@"}
    A_IP=${A_IP%%":"*}
    if [ -z "$A_IP" ];then
        exit 1
    else
        ping -w 3 $A_IP 1>$NULL_DEV 2>&1
        if [ $? -ne 0 ];then
            record_log $LINENO ERROR "Agent ip invalied."
            exit 1
        fi
    fi

    A_USER=${A_ALL%%"@"*}
    if [ -z "$A_USER" ];then
        record_log $LINENO ERROR "Agent user invalied."
        exit 1
    fi

    A_PASS=${A_ALL##*":"}
    if [ -z "$A_PASS" ];then
        record_log $LINENO ERROR "Agent password invalied."
        exit 1
    fi

    exec_cmd "exit 0" $A_IP $A_USER $A_PASS 1>$NULL_DEV 2>&1
    if [ $? -ne 0 ];then
        exit 1
    fi
}

function ready_copy()
{
    exec_scp $BASE_ISO_PATH/${MYVERSION}_TOOLS.tar.gz root@$A_IP:$BASE_ISO_PATH $A_PASS
    pushd $OBS_CACHE_PATH/next/euleros/
        tar cvzf build.tar.gz build
        exec_scp build.tar.gz root@$A_IP:$BASE_ISO_PATH $A_PASS
        rm -rf build.tar.gz
    popd
}

function create_build_for_agent()
{
    while read cmd
    do
        record_log $LINENO INFO "start to execute command $cmd"
        exec_cmd "$cmd" "$A_IP" "$A_USER" "$A_PASS"
        if [ $? -ne 0 ];then
            record_log $LINENO ERROR "Execute command $cmd failed."
            exit -1
        fi
        record_log $LINENO INFO "Execute command $cmd is finished."
    done <<EOF
    cd $BASE_ISO_PATH && tar xf ${MYVERSION}_TOOLS.tar.gz
    cd $BASE_ISO_PATH && tar xf build.tar.gz
    cd $BASE_ISO_PATH/${MYVERSION}_TOOLS && bash -x euleros_server_v200r008c00.sh  create_tar EulerOS_V200R008C00_ARM64 ${MYVERSION} $IP >log 2>&1
    cd $BASE_ISO_PATH && rm -rf ${MYVERSION}_TOOLS* build build.tar.gz
EOF
}

function modify_meta()
{
   local prj_name="$1"
   if [ -f "$prj_name.meta" ];then
      cp -f $prj_name.meta $prj_name.meta-tmp
      line1=`grep -n "<build>" $prj_name.meta-tmp | cut -d \: -f 1`
      line2=`grep -n "</build>" $prj_name.meta-tmp | cut -d \: -f 1`
      sed -i "$line1,${line2}d" $prj_name.meta-tmp
      sed -i "${line1}i\  <build>\n    <disable/>\n  <\/build>" $prj_name.meta-tmp
      osc meta prj $prj_name --file="$prj_name.meta-tmp"
   fi
}

function pkg_meta_reconf()
{
    local prj="$1"
    shift
    local pkgs="$@"
    for pkg in $pkgs
    do
        osc meta pkg $prj $pkg --file="${prj//:/}/$pkg/.osc/_meta"
    done
}

function version_build()
{
    IP="$1"
    A_INFO="$2"
    MYVERSION=$3

    record_log $LINENO INFO "start version_build function."
    SRC_TAR=`ls /opt/*_SOURCE.tar.gz | xargs basename`
    TARGET_TAR=${SRC_TAR%%_SOURCE.tar.gz}
    source_install $SRC_TAR
    sed -i "s/100.109.85.100/$IP/g" /srv/cache/obs/tar_scm/repo/next/euleros/tools/euleros_chroot/euleros_chroot.spec

    get_prj_list EulerOS_Server_V200R008C00 $PRJ_CONF_FILE

    create_build_Fedora 29-1.2 aarch64  armv7l  i686  x86_64
    create_Fedora29_Update Fedora-29-1.2:Updates standard aarch64 armv7l i686  x86_64
#1
    create_prj_safe EulerOS:V3R1:BuildToolsBep
    modify_meta EulerOS:V3R1:BuildToolsBep
    modify_pkg_meta EulerOS:V3R1:BuildToolsBep custom_build_tool build "\  <build>\n    <enable/>\n  <\/build>"
    modify_pkg_meta EulerOS:V3R1:BuildToolsBep gcc_secure build "\  <build>\n    <enable/>\n  <\/build>"
    modify_pkg_meta EulerOS:V3R1:BuildToolsBep bep-env build "\  <build>\n    <enable/>\n  <\/build>"
    wait_build_finish EulerOS:V3R1:BuildToolsBep     
#2
    modify_prj EulerOS:V3R1:BuildToolsBep disable  "Support: custom_build_tool-sign" "Support: custom_build_tool-nocheck"
    modify_pkg_meta EulerOS:V3R1:BuildToolsBep rpm build "\  <build>\n    <enable/>\n  <\/build>"
    modify_pkg_meta EulerOS:V3R1:BuildToolsBep redhat-rpm-config build "\  <build>\n    <enable/>\n  <\/build>"
    sleep 30
    wait_build_finish EulerOS:V3R1:BuildToolsBep
    fixed_prj_failed_pkg EulerOS:V3R1:BuildToolsBep
#3
    modify_pkg_meta EulerOS:V3R1:BuildToolsBep obs-env build "\  <build>\n    <enable/>\n  <\/build>"
    sleep 30
    wait_build_finish EulerOS:V3R1:BuildToolsBep
#4
    modify_prj EulerOS:V3R1:BuildToolsBep disable "Support: custom_build_tool-sign"
    osc meta prj EulerOS:V3R1:BuildToolsBep --file="EulerOS:V3R1:BuildToolsBep.meta"
    pkg_meta_reconf EulerOS:V3R1:BuildToolsBep custom_build_tool gcc_secure bep-env rpm redhat-rpm-config obs-env
    modify_pkg_meta EulerOS:V3R1:BuildToolsBep euleros-release useforbuild "\  <useforbuild>\n    <disable\/>\n  <\/useforbuild>" 
    sleep 30
    wait_build_finish EulerOS:V3R1:BuildToolsBep
#5
    pkg_meta_reconf EulerOS:V3R1:BuildToolsBep euleros-release
    osc meta prjconf EulerOS:V3R1:BuildToolsBep --file="EulerOS:V3R1:BuildToolsBep.prjconf"
    sleep 30
    wait_build_finish EulerOS:V3R1:BuildToolsBep
    fixed_prj_failed_pkg EulerOS:V3R1:BuildToolsBep

    CREATE_PRJ_LISTS=`echo "$PRJ_LIST" | sed "s/\r/\ /g" | sed "s/EulerOS:V3R1:BuildToolsBep/\ /g" | sed "s/Fedora-29-1.2:Updates/\ /g" | sed "s/Fedora-29-1.2/\ /g"`
    create_prj_safe $CREATE_PRJ_LISTS
    for prj_name in $CREATE_PRJ_LISTS
    do
        wait_build_finish $prj_name
    done
    fixed_prj_failed_pkg $CREATE_PRJ_LISTS
    osc rebuild EulerOS:V3R1:STANDARD:Docker golang
    fixed_prj_failed_pkg EulerOS:V3R1:STANDARD:Docker

    analysis_agent $A_INFO
    ready_copy
    create_build_for_agent
    record_log $LINENO INFO "version_build function finish."
}

function create_tar()
{
    CI_PROJECT=$1
    MYVERSION=$2
    IP=$3
    get_create_V2R8_tar_list /opt/${MYVERSION}_TOOLS/$PRJ_CONF_FILE
    cd "/opt/build/script"

    export arm2x86="aarch64"
    export jenkins_build=1
    sed -i "s/OBSSERVER_IP=\"100.109.85.100\"/OBSSERVER_IP=\"$IP\"/g" config.sh

    sed -i "s/100.109.85.100/$IP/g" ../config.xml
    sed -i "s/IP=\"10.120.107.75\"/IP=\"$IP\"/g" internal.sh
    sed -i "s/RELEASE_IP=\"10.120.107.75\"/RELEASE_IP=\"$IP\"/g" config/config_temp
    sed -i "s/MYVERSION=.*/MYVERSION=\"$MYVERSION\"/g" config.sh

    bash make_version.sh set_release_dir

    for pro_name in $CREATE_TAR_LIST
    do
         bash make_version.sh $pro_name
    done
}

function f_main()
{
    arg1=$1
    shift
    arg2=$@
    run_script $arg1 $arg2
}

f_main $@

